package com.tsxy.mall.mapper;

import com.tsxy.mall.entity.Book;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Repository;

import java.text.CollationElementIterator;
import java.util.List;

@Mapper
@Repository
public interface BookMapper {

    @Select({"select * from book"})
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "book_sort_id",property = "bookSortId"),
            @Result(column = "name" ,property = "name"),
            @Result(column="author", property="author", jdbcType=JdbcType.VARCHAR),
            @Result(column = "dscp" ,property = "dscp"),
            @Result(column = "price",property = "price"),
            @Result(column = "version",property = "version"),
            @Result(column = "color",property = "color"),
            @Result(column = "suit",property = "suit"),
            @Result(column = "count",property = "count"),
            @Result(column = "status",property = "status"),
            @Result(column = "id",property = "bookPicture",
                many = @Many(select = "com.tsxy.mall.mapper.BookPictureMapper.getByBookId"))
    })
    List<Book> getAll();

    // List<Book> getBooksBySortId(Integer id);

    @Select({"<script>" +
            "select * from book " +
            "<if test='ids != null and ids.size() > 0'>" +
            " where " +
            " book_sort_id in" +
            "<foreach item='id' index='index' collection='ids' open ='(' separator=',' close= ')' >" +
            " #{ id}" +
            "</foreach>" +
            "</if>" +
            "</script>"})
    @Results({
            @Result(column = "id" ,property = "id"),
            @Result(column = "name" ,property = "name"),
            @Result(column = "id" ,property = "fileUrl",
                many = @Many(select = "com.tsxy.mall.mapper.BookPictureRelationMapper.selectLocalNameByBookId")),
    })
    List<Book> selectInSort(@Param("ids") List<Integer> ids);

    @Select({"<script>"+
            "select book.* from book "+
            "INNER JOIN book_sort on book.book_sort_id = book_sort.id"+
            "<where>"+
            "<if test='book.bookSortId != null ' >book_sort_id = #{book.bookSortId}</if>"+
            "<if test='book.name != null'> and book.name like '%${book.name}%'</if>"+
            "<if test='book.price != null'> and book.price = #{book.price}</if>"+
            "<if test='book.status != null'> and book.status=#{book.status}</if>"+
            "<if test='firstSortId != null and firstSortId != -1' >  and book_sort.parent = #{firstSortId} </if>"+
            "</where>"+
            "</script>"
    })
    @Results({
            @Result(column="id", property="id", jdbcType= JdbcType.INTEGER, id=true),
            @Result(column="book_sort_id", property="bookSortId", jdbcType=JdbcType.INTEGER),
            @Result(column="author", property="author", jdbcType=JdbcType.VARCHAR),
            @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
            @Result(column="dscp", property="dscp", jdbcType=JdbcType.VARCHAR),
            @Result(column="price", property="price", jdbcType=JdbcType.DECIMAL),
            @Result(column="version", property="version", jdbcType=JdbcType.VARCHAR),
            @Result(column="color", property="color", jdbcType=JdbcType.VARCHAR),
            @Result(column="suit", property="suit", jdbcType=JdbcType.VARCHAR),
            @Result(column="count", property="count", jdbcType=JdbcType.INTEGER),
            @Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
            @Result(column = "book_sort_id" ,property = "bookSort",
                    one = @One(select = "com.tsxy.mall.mapper.BookSortMapper.selectByPrimaryKey")),
            @Result(column = "id",property = "fileUrl",
                    many = @Many(select = "com.tsxy.mall.mapper.BookPictureMapper.getByBookId"))
    })
    List<Book> search(@Param("book") Book book, @Param("firstSortId") Integer firstSortId);


    @Select({"select * from book where id = #{id}"})
    @Results({
            @Result(column="id", property="id", jdbcType= JdbcType.INTEGER, id=true),
            @Result(column="book_sort_id", property="bookSortId", jdbcType=JdbcType.INTEGER),
            @Result(column="author", property="author", jdbcType=JdbcType.VARCHAR),
            @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
            @Result(column="dscp", property="dscp", jdbcType=JdbcType.VARCHAR),
            @Result(column="price", property="price", jdbcType=JdbcType.DECIMAL),
            @Result(column="discount", property="disCount", jdbcType=JdbcType.INTEGER),
            @Result(column="version", property="version", jdbcType=JdbcType.VARCHAR),
            @Result(column="color", property="color", jdbcType=JdbcType.VARCHAR),
            @Result(column="suit", property="suit", jdbcType=JdbcType.VARCHAR),
            @Result(column="count", property="count", jdbcType=JdbcType.INTEGER),
            @Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
            @Result(column = "id",property = "fileUrl",
                    many = @Many(select = "com.tsxy.mall.mapper.BookPictureMapper.getByBookId"))
    })
    Book selectById(Integer id);

    @Select({"select * from book where parent = #{sortId}"})
    List<Book> getByBookSort(@Param("sortId") Integer sortId);

    @Update({"update book set count = #{count} where id = #{id}"})
    int updateCount(Book book);
}
